package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbHomeworkhistory;
import com.beetlestech.nfe.main.biz.com.dto.TeacherReplyDTO;
import com.beetlestech.nfe.main.biz.com.dto.VhomeworkhistoryInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.HomeworkhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.MstfileDTO;
import com.beetlestech.nfe.main.db.tbl.MsthomeworkDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.view.VhomeworkhistoryDTO;


public class HbHomeworkhistoryImpl extends AbstractDBLogic implements HbHomeworkhistory, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String HW_DETAIL = "作业详细表";
	protected static final String MST_FILE = "文件表";

	@Override
	public VhomeworkhistoryInfoDTO[] getHomeworkhistoryList(Integer homeworkID) {
		List<VhomeworkhistoryInfoDTO> vhomeworkhistoryList = new ArrayList<VhomeworkhistoryInfoDTO>();

		StatementDTO statementDTO = new StatementDTO();

		// homeworkID
		StatementUtil.andWhere(statementDTO, VhomeworkhistoryDTO.COLUMN_HOMEWORK_ID, StatementUtil.KEYWORD.EQUAL, homeworkID);

		VhomeworkhistoryDTO vhomeworkhistoryDTO = new VhomeworkhistoryDTO();
		vhomeworkhistoryDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());

		// 排序
		vhomeworkhistoryDTO.set_orderByCondition(VhomeworkhistoryDTO.COLUMN_HISTORY_ID);

		List<VhomeworkhistoryDTO> vHomeworkhistorys = queryDAO.queryForObjectList(VhomeworkhistoryDTO.SQL_SEARCH, vhomeworkhistoryDTO);
		for (VhomeworkhistoryDTO item : vHomeworkhistorys) {
			vhomeworkhistoryList.add(convertToHomeworkhistoryInfo(item));
		}

		return vhomeworkhistoryList.toArray(new VhomeworkhistoryInfoDTO[vhomeworkhistoryList.size()]);
	}

	private VhomeworkhistoryInfoDTO convertToHomeworkhistoryInfo(VhomeworkhistoryDTO item) {
		VhomeworkhistoryInfoDTO vhomeworkhistoryInfo = new VhomeworkhistoryInfoDTO();
		vhomeworkhistoryInfo.sethistoryid((item.gethistoryid()));
		vhomeworkhistoryInfo.sethomeworkid(item.gethomeworkid());
		vhomeworkhistoryInfo.sethomeworknm(item.gethomeworknm());
		vhomeworkhistoryInfo.setsenderid(item.getsenderid());
		vhomeworkhistoryInfo.setsendernm(item.getsendernm());
		vhomeworkhistoryInfo.setreceiverid(item.getreceiverid());
		vhomeworkhistoryInfo.setreceivernm(item.getreceivernm());
		vhomeworkhistoryInfo.setfileid(item.getfileid());
		vhomeworkhistoryInfo.setpath(item.getpath());
		vhomeworkhistoryInfo.setlastdate(item.getlastdate());
		vhomeworkhistoryInfo.setlasttime(item.getlasttime());
		vhomeworkhistoryInfo.setmessage(item.getmessage());
		return vhomeworkhistoryInfo;
	}

	@Override
	public void deleteHomeworkhistory(String chkID) {
		HomeworkhistoryDTO deleteHomeworkhistoryDTO = new HomeworkhistoryDTO();
		for (String item : chkID.split(",")) {
			if(!StringUtil.isEmpty(item)) {
				deleteHomeworkhistoryDTO.sethistoryid(Integer.parseInt(item));
				updateDAO.updateOne(HomeworkhistoryDTO.DELETE, deleteHomeworkhistoryDTO, M10190WS, HW_DETAIL);
			}
		}
	}

	@Override
	public void updateHomeworkhistory(Integer homeworkID, Integer senderID,
			Integer receiverID, String message, String path, String forwardSlash) {

		// path转换
		String strPath = path.replaceAll(forwardSlash, "/");

		// mst_file表插入
		MstfileDTO insertMstfileDTO = new MstfileDTO();
		insertMstfileDTO.setpath(strPath);
		insertMstfileDTO.settype(0);
		updateDAO.updateOne(MstfileDTO.INSERT, insertMstfileDTO, M10040WS, MST_FILE);

		// homeworkhistory表插入
		HomeworkhistoryDTO insertHomeworkhistoryDTO = new HomeworkhistoryDTO();
		insertHomeworkhistoryDTO.sethomeworkid(homeworkID);
		insertHomeworkhistoryDTO.setsenderid(senderID);
		insertHomeworkhistoryDTO.setreceiverid(receiverID);
		insertHomeworkhistoryDTO.setfileid(insertMstfileDTO.getfileid());
		insertHomeworkhistoryDTO.setmessage(message);
		insertHomeworkhistoryDTO.setstate(0);
		updateDAO.updateOne(HomeworkhistoryDTO.INSERT, insertHomeworkhistoryDTO, M10040WS, HW_DETAIL);
	}
	//通过
	@Override
	public TeacherReplyDTO getTeacherReplyByhistoryId(Integer historyID){
		HomeworkhistoryDTO historyDTO = new HomeworkhistoryDTO();
		historyDTO.sethistoryid(historyID);
		historyDTO = queryDAO.queryForObject(HomeworkhistoryDTO.EQUAL_SEARCH, historyDTO);
		MsthomeworkDTO homeDTO=new MsthomeworkDTO();
		homeDTO.sethomeworkid(historyDTO.gethomeworkid());
		homeDTO=queryDAO.queryForObject(MsthomeworkDTO.EQUAL_SEARCH, homeDTO);
		MststudentDTO stuDTO=new MststudentDTO();
		stuDTO.setusrid(historyDTO.getsenderid());
		stuDTO=queryDAO.queryForObject(MststudentDTO.EQUAL_SEARCH, stuDTO);
		
		
		TeacherReplyDTO tDto=new TeacherReplyDTO();
		tDto.setHomeTitle(homeDTO.getname());
		tDto.setStuName(stuDTO.getname());
		tDto.setsenderid(historyDTO.getreceiverid());
		tDto.setreceiverid(historyDTO.getsenderid());
		tDto.sethomeworkid(historyDTO.gethomeworkid());
		return tDto;
	}
}
